package com.jianda.service;

import com.jianda.commons.content.Content;
import com.jianda.commons.util.ListSaveUtils;
import com.jianda.view.TextView;
import com.jianda.view.WaveView;
import com.jianda.view.windows.Mainwindows;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * 波形数据控制
 */
public class WaveDataService extends JPanel {
    public static List<Integer> values;                      // 保存接收到的数据的容器.
    public static final int MAX_VALUE = 4959;          // 接收到的数据的最大值.
    public static final int MAX_COUNT_OF_VALUES = 100; // 最多保存数据的个数.

    private static WaveDataService waveDataService;

    private static JFrame jFrame;

    public static WaveDataService getInstance(JFrame jFrames) {
        jFrame = jFrames;
        if (waveDataService == null) {
            synchronized (WaveDataService.class) {
                if (waveDataService == null) {
                    waveDataService = new WaveDataService();
                }
            }
        }
        return waveDataService;
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(Color.BLACK);
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds((int) (jFrame.getWidth() * 0.057), 0, (int) (jFrame.getWidth() * 0.99), (int) (jFrame.getHeight()));
    }

    public WaveDataService() {
         values = Collections.synchronizedList(new ArrayList<Integer>());
         // values = ListSaveUtils.getStorageList("20210613-013827.txt", "/data/wave");

        // 使用一个线程模拟产生数据.
        new Thread(new Runnable() {
            @Override
            public void run() {
                Random rand = new Random();

                try {
                    while (true) {
                        addValue(rand.nextInt(MAX_VALUE)); // 产生一个数据，并模拟接收并放到容器里.
                        repaint();
                        Thread.sleep(200);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 渲染2d样式
        Graphics2D g2d = (Graphics2D) g;
        // RenderingHints.KEY_ANTIALIASING: 全局抗锯齿
        // RenderingHints.VALUE_ANTIALIAS_ON: 抗锯齿呈现模式
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.RED);
       // g2d.drawLine((int) (jFrame.getWidth() * 0.0008), 0, (int) (jFrame.getWidth() * 0.0008), (int)(jFrame.getHeight() * 0.865));
       // g2d.drawLine((int) (jFrame.getWidth() * 0.0008), (int)(jFrame.getHeight() * 0.865), (int) (jFrame.getWidth() * 0.99), (int)(jFrame.getHeight() * 0.865));


        g2d.setClip((int) (jFrame.getWidth() * 0.0010), 0, (int) (jFrame.getWidth() * 0.90), (int) (jFrame.getHeight()));

        // 此组件的当前宽度和高度
        int w = getWidth();
        int h = getHeight() / 10;

        /*Rectangle2D rectangle2D = new Rectangle2D.Float(100, getHeight(), getWidth() - 10, getHeight());
        g2d.draw(rectangle2D);*/

        WaveView.waveView1(w, h, g2d, values, jFrame);
    }

    /**
     * 接收到的数据放入内存.
     * @param value
     */
    private void addValue(int value) {
        // 循环的使用一个接收数据的空间.
        // 最好是实现一个循环数组，而不是偷懒的使用ArrayList.
        if (values.size() > MAX_COUNT_OF_VALUES) {
            values.remove(0);
        }

        values.add(value);
    }
}
